var examListObj = (function ($) {
	var notStartPull,inExamPull,finishPull;
	var notStartPage = 0, inExamPage = 0,finishPage=0,Limit = 20;
	var notStartPulling = false,inExamPulling = false,finishPulling=false;
	var notStartData,inExamData,finishData;
	var pageType = "",taskType='',nurseid=""
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//parr 开始日期^结束日期^查询条件类型^任务类型^考核状态
	//任务类型	T：理论，O:技操，E:应急预案。存在则是检查时获取任务
	//查询条件类型 1:考核主题，2：考核类型，3：考核级别，4：状态
	function getNotStartData(){
		if (notStartPulling) {
			notStartPull.endPullUpToRefresh()
			return;
		}
		notStartPulling = true
		var parr = '^^1^'+taskType+'^2'
		var pdict = {'loginID':nurseid,'input':$('#search')[0].value,'parr':parr,'start':notStartPage,'limit':Limit}
		var paramDict = {'method':'web.INMTeaComm:FindReleaseList','params':JSON.stringify(pdict),'type':'RecQuery'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			if (!dataArr) {
				notStartPull.endPullUpToRefresh();
				notStartPulling = false
				return;
			}
			if (notStartPage == 0) {
				notStartData = dataArr;
				$('#notStartUL')[0].innerHTML = ''
			}else{
				notStartData = notStartData.concat(dataArr)
			}
			
			updateNotStartUI(dataArr)
			if (dataArr.length < Limit) {
				notStartPull.endPullUpToRefresh(true);
			}else{
				notStartPull.endPullUpToRefresh();
			}
			notStartPage += dataArr.length;
			notStartPulling = false
		}, function(errorStr) {
		 	notStartPull.endPullUpToRefresh();
		 	notStartPulling = false
		});
	}
	function updateNotStartUI(dataArr){
		var notStartUL = document.getElementById("notStartUL");
		var oldLis = notStartUL.querySelectorAll('li')
		var dict = {'L':'科级','H':'院级','W':'病区'}
		var dateTime = new Date().getTime()
		for(var i=0; i < dataArr.length; i++){
			var oneData = dataArr[i]
			var level = oneData['ReleaseLevel']
			level = dict[level]
			var timeList = oneData['TimeList'].split('「') //多个批次「
			var oneTime = timeList[0].split('」') //开始日期」截止日期」预约截止时间」考核时间」考核地点」人数上限
			var html = '<div class="classTitle">'+oneData['ReleaseDesc']+'</div>'+
				'<div class="classInfo">'+level+' | '+oneData['Duration']+'分钟</div>'
			if (pageType == '技操') {
				var orderNum = oneData['OrderNum']==''?1:parseInt(oneData['OrderNum'])
				oneTime = timeList[orderNum - 1].split('」')
				var orderTime = oneTime[2]
				html += '<div class="classSubInfo">考核日期：'+oneTime[0]+' - '+oneTime[1]+'</div>'+
					'<div class="classSubInfo">考核地点：'+(oneTime[4]==undefined?'':oneTime[4])+'</div>'
				//预约截止日期
				if (orderTime != "") {
					orderTime = new Date(orderTime).getTime()
					if (orderTime >= dateTime) {
						if (oneData['OrderNum']=='') {
							html += '<div class="mui-btn mui-btn-primary baseBtn">预约</div>'
						}else{
							html += '<div class="mui-btn mui-btn-primary baseBtn changeOrder">修改预约</div>'
						}
					}
				}
			}else{
				html += '<div class="classInfo">考核日期：'+oneTime[0]+' 至 '+oneTime[1]+'</div>'
			}
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.id = oldLis.length+i + '-li0'
			newLi.innerHTML = html
			notStartUL.appendChild(newLi)
			var changeOrder = newLi.querySelector('.baseBtn')
			if (changeOrder) {
				changeOrder.addEventListener('tap',function(){
					var index = parseInt(this.parentNode.id)
					var data = notStartData[index]
					var webview = plus.webview.create('toOrder.html', 'toOrder', {
						background: '#fff',
						statusbar: {
							background: '#fff'
						}
					},{sourceData:data,pageType:'examine'});
					webview.show('fade-in');
					webview.addEventListener('close',resetNotStartRefresh)
				})
			}
		}
	}
	//考核中
	function getInExamData(){
		if (inExamPulling) {
			inExamPull.endPullUpToRefresh()
			return;
		}
		inExamPulling = true
		
		var parr = '^^1^'+taskType+'^1'
		var pdict = {'loginID':nurseid,'input':$('#search')[0].value,'parr':parr,'start':inExamPage,'limit':Limit}
		var paramDict = {'method':'web.INMTeaComm:FindReleaseList','params':JSON.stringify(pdict),'type':'RecQuery'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			if (!dataArr) {
				inExamPull.endPullUpToRefresh();
				inExamPulling = false
				return;
			}
			if (inExamPage == 0) {
				inExamData = dataArr;
				$('#inExamUL')[0].innerHTML = ''
			}else{
				inExamData = inExamData.concat(dataArr)
			}
			
			if (pageType == '技操') {
				updateJiCaoInExamUI(dataArr);
			}else{
				updateInExamUI(dataArr)
			}
			if (dataArr.length < Limit) {
				inExamPull.endPullUpToRefresh(true);
			}else{
				inExamPull.endPullUpToRefresh();
			}
			inExamPage += dataArr.length;
			inExamPulling = false
		}, function(errorStr) {
		 	inExamPull.endPullUpToRefresh();
		 	inExamPulling = false
		});
	}
	function updateInExamUI(dataArr){
		var inExamUL = document.getElementById("inExamUL");
		var oldLis = inExamUL.querySelectorAll('li')
		var dict = {'L':'科级','H':'院级','W':'病区'}
		for(var i=0; i<dataArr.length; i++){
			var oneData = dataArr[i]
			var level = oneData['ReleaseLevel']
			level = dict[level]
			var timeList = oneData['TimeList'].split('」')
			
			var html = '<div class="classTitle">'+oneData['ReleaseDesc']+'</div>'+
				'<div class="classInfo">'+level+' | '+oneData['Duration']+'分钟</div>'+
				'<div class="classInfo">考核日期：'+timeList[0]+' - '+timeList[1]+'</div>'
			
			if(i%4 == 0){
				html += '<div class="mui-btn mui-btn-primary baseBtn">去考核</div>'
			}else if (i%4 == 1) {
				html += '<div class="scoreArea">考核时间：2021.06.18 <div class="scoreText waitting">等待阅卷</div></div>'
			}else if (i%4 == 2) {
				html += '<div class="scoreArea">考核时间：2021.06.18 <div class="scoreText">成绩：55</div></div>'+
					'<div class="scoreImg nurfont nur-notPass"></div>'+
					'<div class="mui-btn mui-btn-primary baseBtn retry">重考(2)</div>'
			}else{
				html += '<div class="scoreImg nurfont nur-pass"></div>'+
					'<div class="scoreArea">考核时间：2021.06.18 <div class="scoreText">成绩：95</div></div>';
			}
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.id = oldLis.length+i + '-li1'
			newLi.innerHTML = html
			inExamUL.appendChild(newLi)
			newLi.addEventListener('tap',tableViewCellTapped)
		}
	}
	
	function tableViewCellTapped(){
		var index = parseInt(this.id)
		var data = inExamData[index]
		var webview = plus.webview.create('toExamine.html', 'toExamine', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{fromPage:'examList',pageType:pageType,sourceData:data});
		webview.show('fade-in');
	}
	function updateJiCaoInExamUI(dataArr){
		var inExamUL = document.getElementById("inExamUL");
		var dict = {'L':'科级','H':'院级','W':'病区'}
		for(var i=0; i<dataArr.length; i++){
			var oneData = dataArr[i]
			var level = oneData['ReleaseLevel']
			level = dict[level]
			var timeList = oneData['TimeList'].split('「') //多个批次「
			var orderNum = oneData['OrderNum']==''?1:parseInt(oneData['OrderNum'])
			var oneTime = timeList[orderNum - 1].split('」') //开始日期」截止日期」预约截止时间」考核时间」考核地点」人数上限 
			var html = '<div class="classTitle">'+oneData['ReleaseDesc']+'</div>'+
				'<div class="classInfo">'+level+' | '+oneData['Duration']+'分钟</div>'+
				'<div class="classSubInfo">考核日期：'+oneTime[0]+' - '+oneTime[1]+'</div>'+
				'<div class="classSubInfo">考核地点：'+(oneTime[4]==undefined?'':oneTime[4])+'</div>'
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.innerHTML = html
			inExamUL.appendChild(newLi)
		}
	}
	//考核结束
	function getFinishData(){
		if (finishPulling) {
			finishPull.endPullUpToRefresh()
			return;
		}
		finishPulling = true
		var parr = '^^1^'+taskType+'^3'
		var pdict = {'loginID':nurseid,'input':$('#search')[0].value,'parr':parr,'start':finishPage,'limit':Limit}
		var paramDict = {'method':'web.INMTeaComm:FindReleaseList','params':JSON.stringify(pdict),'type':'RecQuery'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			if (!dataArr) {
				finishPull.endPullUpToRefresh();
				finishPulling = false
				return;
			}
			if (finishPage == 0) {
				finishData = dataArr;
				$('#finishUL')[0].innerHTML = ''
			}else{
				finishData = finishData.concat(dataArr)
			}
			
			if (pageType == '技操') {
				updateJiCaoFinishUI(dataArr);
			}else{
				updateFinishUI(dataArr)
			}
			if (dataArr.length < Limit) {
				finishPull.endPullUpToRefresh(true);
			}else{
				finishPull.endPullUpToRefresh();
			}
			finishPage += dataArr.length;
			finishPulling = false
		}, function(errorStr) {
		 	finishPull.endPullUpToRefresh();
		 	finishPulling = false
		});
	}
	function updateFinishUI(dataArr){
		var finishUL = document.getElementById("finishUL");
		var dict = {'L':'科级','H':'院级','W':'病区'}
		for(var i=0; i < dataArr.length; i++){
			var oneData = dataArr[i]
			var level = oneData['ReleaseLevel']
			level = dict[level]
			var timeList = oneData['TimeList'].replace('」',' - ')
			var html = '<div class="classTitle">'+oneData['ReleaseDesc']+'</div>'+
				'<div class="classInfo">'+level+' | '+oneData['Duration']+'分钟</div>'+
				'<div class="classInfo">考核日期：'+timeList+'</div>'
			if(i%4 == 0){
				html += '<div class="scoreArea">考核时间：2021.06.18 <div class="scoreText waitting">等待阅卷</div></div>'
			}else if (i%4 == 1) {
				html += '<div class="scoreImg nurfont nur-quekao"></div>'
			}else if (i%4 == 2) {
				html += '<div class="scoreArea">考核时间：2021.06.18 <div class="scoreText">成绩：55</div></div>'+
					'<div class="scoreImg nurfont nur-notPass"></div>'
			}else{
				html += '<div class="scoreImg nurfont nur-pass"></div>'+
					'<div class="scoreArea">考核时间：2021.06.18 <div class="scoreText">成绩：95</div></div>';
			}
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.innerHTML = html
			finishUL.appendChild(newLi)
		}
	}
	
	function updateJiCaoFinishUI(dataArr){
		var finishUL = document.getElementById("finishUL");
		var dict = {'L':'科级','H':'院级','W':'病区'}
		for(var i=0; i<dataArr.length; i++){
			var oneData = dataArr[i]
			var level = oneData['ReleaseLevel']
			level = dict[level]
			var timeList = oneData['TimeList'].split('「') //多个批次「
			var orderNum = oneData['OrderNum']==''?1:parseInt(oneData['OrderNum'])
			var oneTime = timeList[orderNum - 1].split('」') //开始日期」截止日期」预约截止时间」考核时间」考核地点」人数上限 
			var html = '<div class="classTitle">'+oneData['ReleaseDesc']+'</div>'+
				'<div class="classInfo">'+level+' | '+oneData['Duration']+'分钟</div>'+
				'<div class="classSubInfo">考核日期：'+oneTime[0]+' - '+oneTime[1]+'</div>'+
				'<div class="classSubInfo">考核地点：'+(oneTime[4]==undefined?'':oneTime[4])+'</div>'
			if (i%4 == 1) {
				html += '<div class="scoreImg nurfont nur-quekao"></div>'
			}else if (i%4 == 2) {
				html += '<div class="scoreArea">用时：00:24:12<div class="scoreText">成绩：55</div></div>'+
					'<div class="scoreImg nurfont nur-notPass"></div>'
			}else{
				html += '<div class="scoreImg nurfont nur-pass"></div>'+
					'<div class="scoreArea">用时：00:24:12<div class="scoreText">成绩：95</div></div>';
			}
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.innerHTML = html
			finishUL.appendChild(newLi)
		}
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		if (mui.os.plus) {
			var self = plus.webview.currentWebview()
			// sourceData = self.sourceData;
			pageType = self.pageType;
			taskType = (pageType=="技操")?'O':'T'
			// fromPage = self.fromPage;
			$('.mui-title')[0].innerHTML = pageType + '考核'
			
		}
		var perInfo = app.getState()
		nurseid = perInfo['loginID']
		mui("body").on("tap", ".searchArea .mui-icon-clear", function() {
			if (document.getElementById('search').value != "") {
				document.getElementById('search').value = ""
			}
			refreshMainPage()
		});
		var slider = document.getElementById('slider');
		slider.addEventListener('slide', function(event) {
			var index = event.detail.slideNumber;
			refreshMainPage(index)
			$('#sliderIndicator .mui-indicator[data-index="'+index+'"]')[0].classList.add('mui-active');
		});
		var indicators = document.querySelectorAll('#sliderIndicator .mui-indicator');
		for (var i = 0; i < indicators.length;i++) {
			indicators[i].addEventListener('tap',function(){
				var oldActive = $('#sliderIndicator .mui-indicator.mui-active')[0];
				if (oldActive) {
					if (oldActive == this) {
						return
					}
					oldActive.classList.remove('mui-active')
				}
				var newIndex = parseInt(this.getAttribute('data-index'))
				refreshMainPage(newIndex)
				this.classList.add('mui-active')
				mui('#slider').slider().gotoItem(newIndex,100)
			});
		}
		var search = document.getElementById('search');
		search.addEventListener('input',function(){
			refreshMainPage()
		});
		//待考核
		var notStartScroll = document.getElementById('notStartScroll');
		notStartPull = $(notStartScroll).pullToRefresh({
			up: {
				callback: function() {
					getNotStartData()
				}
			}
		});
		//考核中
		var inExamScroll = document.getElementById('inExamScroll');
		inExamPull = $(inExamScroll).pullToRefresh({
			up: {
				callback: function() {
					getInExamData()
				}
			}
		});
		//考核结束
		var finishScroll = document.getElementById('finishScroll');
		finishPull = $(finishScroll).pullToRefresh({
			up: {
				callback: function() {
					getFinishData()
				}
			}
		});
		getNotStartData();
		getInExamData();
		getFinishData();
	}
	function refreshMainPage(newIndex){
		if (newIndex == undefined) {
			resetNotStartRefresh();
			resetInExamRefresh()
			resetFinishRefresh()
			return
		}
		if (newIndex == 0){
			resetNotStartRefresh();
		}else if(newIndex == 1){
			resetInExamRefresh()
		}else{
			resetFinishRefresh()
		}
	}
	function resetNotStartRefresh(){
		notStartPage = 0;
		notStartData = []
		if (notStartPull) {
			notStartPull.refresh(true)
		}
		getNotStartData();
		$('#notStartWrapper').scroll().scrollTo(0,0,100);
	}
	function resetInExamRefresh(){
		inExamPage = 0;
		inExamData = []
		if (inExamPull) {
			inExamPull.refresh(true)
		}
		getInExamData();
		$('#inExamWrapper').scroll().scrollTo(0,0,100);
	}
	function resetFinishRefresh(){
		finishPage = 0;
		finishData = []
		if (finishPull) {
			finishPull.refresh(true)
		}
		getFinishData();
		$('#finishWrapper').scroll().scrollTo(0,0,100);
	}
	var refreshPageData = refreshMainPage
	var exportObj = {}
	exportObj.refreshPageData = refreshPageData
	return exportObj
})(mui);

function refreshPageData(){
	examListObj.refreshPageData()
}
/*
"rw": "1",
	"CreatDate": "2020-10-27",	创建日期
	"CreatTime": "15:32:19",	创建时间
	"Creator": "0",				创建人
	"Duration": "90",			考核时长
	"Examinee": "护士层级",		考核人群类别
	"ExamineeWard": "",			考核病区
	"ExamineeWardDesc": "",
	"MarkShowFlag": "",					阅卷是否显示考生信息
	"Markor": "",						阅卷人
	"PaperType": "I",					出卷方式 I:智能,H:手动,C:调用试卷,
	"QuestRangeI": "6@%Collection.ListOfDT",	智能组卷-固定组卷
	"RealPaperType": "I",				出卷实际方式 I:智能,H:手动
	"ReleaseDesc": "内科基本护理",		考核主题
	"ReleaseDraw": "Y",					是否固定试卷：Y:是，N:否
	"ReleaseLevel": "L",				考核级别：H:院级,L:科级,W:病区
	"ReleaseMode": "T",					隶属类型：类论:T、技操:O、应急预案:E
	"ReleasePaper": "",					试卷
	"ReleasePass": "60",				及格成绩
	"ReleaseResit": "N",				是否可重考N:否，Y:是
	"ReleaseResitNum": "",				重考次数：1、2、3、N:不限定
	"Score": "24",						总分
	"Status": "Y",					状态:N:保存,Y:发布,
	"StatusDesc": "已发布",
	"ReleaseType": "54||3",			隶属项目
	"ReleaseTypeDesc": "专科",		隶属项目描述
	"Examiner": "21,1,26",						考官
	"ExaminerDesc": "刘润潼,冯亮,汪雨铃",			考官姓名
	"ExamineeList": "19||1,19||2,19||3",		考核人群类型
	"ExamineeListDesc": "护士层级:N0,N1,N2",		考核人群类型描述
	"QuestRangeEmr": "",						考核范围--流程图
	"TimeList": "2020-10-28」2020-10-31",		考核时间多个
	"ExamStep": "3", //根据时间决定考试状态:2 未开始考核;3 考核结束
	"QuestRange": "S」53||1」6」4」24「M」53||4」0」0」",		考核范围
	"OrderNum": ""				预约批次
	
	/// 关联培训【暂时不管】
	Property ReleaseTrain As list Of %String [ SqlColumnNumber = 11, SqlFieldName = ReleaseTrain ];
*/